home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / midi / gfft.lha / gfft-2.03 / source / gfft-2.03-source.lha / okplot.c < prev    next >
C/C++ Source or Header  |  1996-01-02  |  9KB  |  290 lines

  1. /***************************************************************************
  2.  *          Copyright (C) 1994  Charles P. Peterson                  *
  3.  *         4007 Enchanted Sun, San Antonio, Texas 78244-1254             *
  4.  *              Email: Charles_P_Peterson@fcircus.sat.tx.us                *
  5.  *                                                                         *
  6.  *          This is free software with NO WARRANTY.                  *
  7.  *          See gfft.c, or run program itself, for details.              *
  8.  *              Support is available for a fee.                      *
  9.  ***************************************************************************
  10.  *
  11.  * Program:     gfft--General FFT analysis
  12.  * File:        ok_plot.c
  13.  * Purpose:     plot data (invoking separate plotter program)
  14.  * Author:      Charles Peterson (CPP)
  15.  * History:     24-November-1993 CPP; Created.
  16.  *              14-July-1994 CPP (1.01); Give helpful notes on failure.
  17.  *              9-Aug-94 CPP (1.10); Hidden3D now optional
  18.  *              28-Aug-94 CPP (1.12); Use Z for Y in 3D
  19.  *              12-Oct-94 CPP (1.15); Fix for log x in gnuplot, also correct
  20.  *                precision for xrange specifications
  21.  *              17-Jan-95 CPP (1.20); Size GNUPLOT shell nicely
  22.  *               9-Feb-95 CPP (1.33); Allow multiple GNUPLOT sessions
  23.  *              14-Feb-95 CPP (1.42); Allow freeform PlotCommands
  24.  *
  25.  * Comment:     Only gnuplot supported now; others may be added.
  26.  *              There may be some system dependency here.
  27.  *              Eventually, it is intended to be made system independent.
  28.  *              Also, might add custom 'gfft' plotter.
  29.  */
  30.  
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34.  
  35. #ifdef AMIGA
  36. #include <dos/dos.h>
  37. #include <clib/dos_protos.h>
  38. #endif
  39.  
  40. #include "gfft.h"
  41. #include "settings.h"
  42.  
  43. #define GPLOT_3D_LOG_BUG_WORKAROUND
  44.  
  45.  
  46. int Old_File_Count = 0;  /* This may be read or reset to zero externally */
  47. static char **Old_File_Name = NULL;
  48. static BOOLEAN plotted_already = FALSE;
  49. static BOOLEAN re_plotting = FALSE;
  50.  
  51. extern double LowestFrequencyWritten;
  52. extern double HighestFrequencyWritten;
  53.  
  54. #ifdef AMIGA
  55. extern int Shell_Height;
  56. extern int Shell_Width;
  57. #endif
  58.  
  59. static void do_ok_plot (BOOLEAN this_is_re_plot);
  60.  
  61. void do_re_plot (void)
  62. {
  63. /* Further checking could be included here */
  64.     re_plotting = TRUE;
  65.     do_ok_plot (TRUE);
  66. }
  67.  
  68. void ok_plot (void)
  69. {
  70.     re_plotting = FALSE;
  71.     do_ok_plot (FALSE);
  72. }
  73.  
  74. static void do_ok_plot (BOOLEAN this_is_re_plot)
  75. {
  76.     static name_count = 0;
  77.     char buffer[COMMAND_BUFFER_SIZE];
  78.     FILE *comfile_ptr;
  79.     FILE *gnuplot_comfile_ptr;
  80.     char *save_name;
  81.     double xrange[2];
  82.     BOOLEAN restrict_xrange = FALSE;
  83.     char plot_command_file_name[MAX_PLOT_COMMANDFILE_NAME];
  84.     char gnuplot_command_file_name[MAX_PLOT_COMMANDFILE_NAME];
  85.  
  86.     plotted_already = TRUE;
  87.     xrange[0] = LowestFrequencyWritten;
  88.     xrange[1] = HighestFrequencyWritten;
  89.  
  90.     sprintf (plot_command_file_name, "%s%d", PLOT_COMMAND_FILE_NAME, 
  91.          ++name_count);
  92.     sprintf (gnuplot_command_file_name, "%s%d", GNUPLOT_COMMAND_FILE_NAME,
  93.          name_count);
  94.  
  95. /**************************************************************************\
  96.  * Write command file
  97. \**************************************************************************/
  98.  
  99.     comfile_ptr = fopen (plot_command_file_name, "w");
  100.     if (!comfile_ptr)
  101.     {
  102.     error_message (CANT_CREATE_PLOTTER_FILE);
  103.     RAISE_ERROR (NOTHING_SPECIAL);  /* longjmp outa here! */
  104.     }
  105.  
  106. #ifdef AMIGA
  107.     fprintf (comfile_ptr, "stack %d\n", GNUPLOT_STACKSIZE);
  108.     fprintf (comfile_ptr,"echo \" \"\n");
  109.     fprintf (comfile_ptr,
  110. "echo \" Press <RETURN> to end plot display once it appears.\"\n");
  111.     fprintf (comfile_ptr,"echo \" \"\n");
  112.     fprintf (comfile_ptr,"%s%s%s",
  113. "echo \" ",
  114. "(All is OK now, but if gnuplot has a problem later, try fewer bins.)",
  115. "\"\n");
  116.     fprintf (comfile_ptr,"echo \" \"\n");
  117.     fprintf (comfile_ptr,"echo \" Generating plot using GNUPLOT\"\n");
  118.     fprintf (comfile_ptr,"echo \" \"\n");
  119.     fprintf (comfile_ptr,
  120. "echo \" Copyright(C) 1986 - 1993   Thomas Williams, Colin Kelley\"\n");
  121.     fprintf (comfile_ptr,"wait 1\n");
  122.     fprintf (comfile_ptr,"failat 1\n");  /* exit script on gnuplot error */
  123. #endif
  124.  
  125.     fprintf (comfile_ptr,"gnuplot %s\n",
  126.              gnuplot_command_file_name);
  127.  
  128. #ifdef AMIGA
  129.     fprintf (comfile_ptr,"delete \"%s\" QUIET\n",gnuplot_command_file_name);
  130.     fprintf (comfile_ptr,"endcli\n");
  131. #endif
  132.  
  133.     fclose (comfile_ptr);
  134.  
  135. /**************************************************************************\
  136.  * Write GNUPLOT command file
  137. \**************************************************************************/
  138.  
  139.     gnuplot_comfile_ptr = fopen (gnuplot_command_file_name,"w");
  140.     if (!gnuplot_comfile_ptr)
  141.     {
  142.     error_message (CANT_CREATE_PLOTTER_FILE);
  143.     RAISE_ERROR (NOTHING_SPECIAL);  /* longjmp outa here! */
  144.     }
  145.     if (Terminal && Terminal != NullString && strlen(Terminal))
  146.     {
  147.     fprintf (gnuplot_comfile_ptr, "set terminal %s\n", Terminal);
  148.     }
  149.     if (PlotOutput && PlotOutput != NullString && strlen(PlotOutput))
  150.     {
  151.     fprintf (gnuplot_comfile_ptr, "set output \'%s\'\n", PlotOutput);
  152.     }
  153.     if (!Time3D)
  154.     {
  155.     if (LowY != LOWEST_Y) fprintf (gnuplot_comfile_ptr, 
  156.                        "set yrange [%g:]\n", LowY);
  157.     if (HighY != HIGHEST_Y) fprintf (gnuplot_comfile_ptr, 
  158.                      "set yrange [:%g]\n", HighY);
  159.     }
  160.     else   /* in 3d, amplitude is z */
  161.     {
  162.     if (LowY != LOWEST_Y) fprintf (gnuplot_comfile_ptr, 
  163.                        "set zrange [%g:]\n", LowY);
  164.     if (HighY != HIGHEST_Y) fprintf (gnuplot_comfile_ptr, 
  165.                      "set zrange [:%g]\n", HighY);
  166.     }
  167.     if (LogX) fprintf (gnuplot_comfile_ptr, "set log x\n");
  168.     if (LogY && !Time3D) fprintf (gnuplot_comfile_ptr, "set log y\n");
  169.     if (LogY && Time3D) fprintf (gnuplot_comfile_ptr, "set log z\n");
  170.     if (RotX != DEF_ROT_X)
  171.     {
  172.     fprintf (gnuplot_comfile_ptr, "set view %g\n", RotX);
  173.     }
  174.     if (RotZ != DEF_ROT_Z)
  175.     {
  176.     fprintf (gnuplot_comfile_ptr, "set view ,%g\n", RotZ);
  177.     }
  178.     if (LowFrequency != LOWEST_FREQUENCY &&
  179.     HighFrequency != HIGHEST_FREQUENCY)
  180.     {
  181.     restrict_xrange = TRUE;
  182.     xrange[0] = LowFrequency;
  183.     xrange[1] = HighFrequency;
  184.     }
  185.     else if (LowFrequency != LOWEST_FREQUENCY)
  186.     {
  187.     restrict_xrange = TRUE;
  188.     xrange[0] = LowFrequency;
  189.     }
  190.     else if (HighFrequency != HIGHEST_FREQUENCY)
  191.     {
  192.     restrict_xrange = TRUE;
  193.     xrange[1] = HighFrequency;
  194.     }
  195.     if (this_is_re_plot && restrict_xrange)
  196.     {
  197. #ifdef _FFP
  198.     fprintf (gnuplot_comfile_ptr, "set xrange [%-15.8g:%-15.8g]\n",
  199.          xrange[0], xrange[1]);
  200. #else
  201.     fprintf (gnuplot_comfile_ptr, "set xrange [%-19.12g:%-19.12g]\n",
  202.          xrange[0], xrange[1]);
  203. #endif
  204.     }
  205.  
  206.     write_plot_commands (gnuplot_comfile_ptr, NullString, NullString,
  207.              PlotCommands);
  208.  
  209.     if (Time3D)
  210.     {
  211.     fprintf (gnuplot_comfile_ptr, "set parametric\n");
  212.     if (Hidden3D)
  213.     {
  214.         fprintf (gnuplot_comfile_ptr, "set hidden3d\n");
  215.     }
  216.  
  217.  
  218. #ifdef GPLOT_3D_LOG_BUG_WORKAROUND
  219. /*
  220.  * This works around a bug in GNUPLOT 3.5 and below:
  221.  * log x caused crash in 3d unless urange was set beforehand
  222.  */
  223.     if (LogX)
  224.     {
  225.         fprintf (gnuplot_comfile_ptr,
  226. "# The following line is a workaround for a bug in GNUPLOT 3.5\n");
  227.  
  228.        fprintf (gnuplot_comfile_ptr, "set urange [1:2]\n");
  229.     }
  230. #endif
  231.  
  232.     fprintf (gnuplot_comfile_ptr, "splot ");
  233.     }
  234.     else
  235.     {
  236.     fprintf (gnuplot_comfile_ptr, "plot ");
  237.     }
  238.     if (CombinePlots)
  239.     {
  240.     int i;
  241.  
  242.     if (re_plotting && Old_File_Count)
  243.     {
  244.         if (strlen (OkWriteName) ==
  245.         strlen (Old_File_Name[Old_File_Count - 1]))
  246.         {
  247.         if (!strcmp(OkWriteName,Old_File_Name[Old_File_Count - 1]))
  248.         {
  249.             Old_File_Count--;
  250.         }
  251.         }
  252.     }
  253.  
  254.     for (i = 0; i < Old_File_Count; i++)
  255.     {
  256.         fprintf (gnuplot_comfile_ptr, "\'%s\' with lines,",
  257.              Old_File_Name[i]);
  258.     }
  259.     }
  260.     fprintf (gnuplot_comfile_ptr, "\'%s\' with lines\n",OkWriteName);
  261.     fclose (gnuplot_comfile_ptr);
  262.  
  263.     save_name = gmalloc (strlen(OkWriteName) + 1, NOTHING_SPECIAL);
  264.     strcpy (save_name, OkWriteName);
  265.     Old_File_Count = (CombinePlots) ? ++Old_File_Count : 1;
  266.  
  267.     Old_File_Name = grealloc (Old_File_Name, Old_File_Count *
  268.                   sizeof (char *), NOTHING_SPECIAL);
  269.     Old_File_Name[Old_File_Count-1] = save_name;
  270.  
  271.  
  272. /**************************************************************************\
  273.  * Write shell command
  274. \**************************************************************************/
  275.  
  276. #ifdef AMIGA
  277.     sprintf (buffer,
  278.          "newcli CON:0/0/%d/%d/Gfft-Gnuplot-Shell from %s", 
  279.          Shell_Width, Shell_Height, plot_command_file_name);
  280. #else
  281.     sprintf (buffer, "%s", plot_command_file_name);
  282. #endif
  283.  
  284. /**************************************************************************\
  285.  * Execute shell command
  286. \**************************************************************************/
  287.  
  288.     system (buffer);
  289. }
  290.